package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.CreateGoodsBO;
import com.cskaoyan.bean.bo.GoodsParam;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.CatAndBrandVO;
import com.cskaoyan.bean.vo.FirstCategoryVO;
import com.cskaoyan.bean.vo.GoodsDetailVO;
import com.cskaoyan.bean.vo.SecondCategoryOrBrandVO;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.GoodsService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: harveyliu
 * @description: GoodsService接口的实现类
 * @time: 2021/7/8 20:53
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    MallGoodsMapper mallGoodsMapper;

    @Autowired
    MallCategoryMapper mallCategoryMapper;

    @Autowired
    MallBrandMapper mallBrandMapper;

    @Autowired
    MallGoodsAttributeMapper mallGoodsAttributeMapper;

    @Autowired
    MallGoodsSpecificationMapper mallGoodsSpecificationMapper;

    @Autowired
    MallGoodsProductMapper mallGoodsProductMapper;



    /**
     * @Author harvey
     * @description: 用来查询商品列表，有分页的要求，还有可以根据编号和商品名称的模糊查询
     * @Date  2021/7/8 21:10
     * url: http://localhost:8083/admin/goods/list?page=1&limit=20&sort=add_time&order=desc
     */
    @Override
    public BaseData<MallGoods> queryUserByConditions(GoodsParam param) {
        PageHelper.startPage(param.getPage(),param.getLimit());
        MallGoodsExample mallGoodsExample = new MallGoodsExample();
        MallGoodsExample.Criteria criteria = mallGoodsExample.createCriteria();
        if (param.getGoodsSn() != null) {
            criteria.andGoodsSnEqualTo(String.valueOf(param.getGoodsSn()));
        }
        if (param.getName() != null) {
            criteria.andNameLike("%" + param.getName() + "%");
        }
        criteria.andDeletedEqualTo(false);
        mallGoodsExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MallGoods> goodsList = mallGoodsMapper.selectByExampleWithBLOBs(mallGoodsExample);
        int total = (int)mallGoodsMapper.countByExample(mallGoodsExample);
        return BaseData.ok(goodsList, total);
    }

    /**
     * @Author harvey
     * @description: 用来查找所有的商品分类
     * @Date 2021/7/8 22:41
     * url: http://localhost:8083/admin/goods/catAndBrand
     */
    @Override
    public CatAndBrandVO queryAllCategory() {
        MallCategoryExample mallCategoryExample = new MallCategoryExample();
        MallCategoryExample.Criteria criteria = mallCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(0);
        List<MallCategory> firstCategories = mallCategoryMapper.selectByExample(mallCategoryExample);
        List<FirstCategoryVO> firstCategoryVOList = new ArrayList<>();
        for (MallCategory firstCategory : firstCategories) {
            MallCategoryExample mallCategoryExample2 = new MallCategoryExample();
            MallCategoryExample.Criteria criteria2 = mallCategoryExample2.createCriteria();
            criteria2.andPidEqualTo(firstCategory.getId());
            criteria2.andDeletedEqualTo(0);
            List<MallCategory> secondCategories = mallCategoryMapper.selectByExample(mallCategoryExample2);
            List<SecondCategoryOrBrandVO> secondCategoryVOList = new ArrayList<>();
            for (MallCategory secondCategory : secondCategories) {
                SecondCategoryOrBrandVO secondCategoryVO = new SecondCategoryOrBrandVO(secondCategory.getId(), secondCategory.getName());
                secondCategoryVOList.add(secondCategoryVO);
            }
            FirstCategoryVO firstCategoryVO = new FirstCategoryVO(firstCategory.getId(), firstCategory.getName(), secondCategoryVOList);
            firstCategoryVOList.add(firstCategoryVO);
        }

        MallBrandExample mallBrandExample = new MallBrandExample();
        List<MallBrand> mallBrands = mallBrandMapper.selectByExample(mallBrandExample);
        List<SecondCategoryOrBrandVO> secondCategoryOrBrandVOList = new ArrayList<>();
        for (MallBrand mallBrand : mallBrands) {
            SecondCategoryOrBrandVO secondCategoryOrBrandVO = new SecondCategoryOrBrandVO(mallBrand.getId(), mallBrand.getName());
            secondCategoryOrBrandVOList.add(secondCategoryOrBrandVO);
        }
        return new CatAndBrandVO(firstCategoryVOList, secondCategoryOrBrandVOList);
    }


    /**
     * @Author harvey
     * @description: 通过商品的id来得到商品的详情
     * @Date 2021/7/9 8:36
     * url:http://182.92.235.201:8083/admin/goods/detail?id=1181012
     */
    @Override
    public GoodsDetailVO queryGoodsDetailByGoodsId(Integer id) {
        MallGoods mallGoods = mallGoodsMapper.selectByPrimaryKey(id);
        MallCategory secondCategory = mallCategoryMapper.selectByPrimaryKey(mallGoods.getCategoryId());
        MallCategory firstCategory = null;
        List<Integer> categoryIds = new ArrayList<>();
        if (secondCategory != null) {
            firstCategory = mallCategoryMapper.selectByPrimaryKey(secondCategory.getPid());
            categoryIds.add(firstCategory.getId());
            categoryIds.add(secondCategory.getId());
        }

        MallGoodsAttributeExample mallGoodsAttributeExample = new MallGoodsAttributeExample();
        MallGoodsAttributeExample.Criteria mallGoodsAttributeExampleCriteria = mallGoodsAttributeExample.createCriteria();
        mallGoodsAttributeExampleCriteria.andGoodsIdEqualTo(id);
        mallGoodsAttributeExampleCriteria.andDeletedEqualTo(false);
        List<MallGoodsAttribute> mallGoodsAttributes = mallGoodsAttributeMapper.selectByExample(mallGoodsAttributeExample);

        MallGoodsSpecificationExample mallGoodsSpecificationExample = new MallGoodsSpecificationExample();
        MallGoodsSpecificationExample.Criteria mallGoodsSpecificationExampleCriteria = mallGoodsSpecificationExample.createCriteria();
        mallGoodsSpecificationExampleCriteria.andGoodsIdEqualTo(id);
        mallGoodsSpecificationExampleCriteria.andDeletedEqualTo(false);
        List<MallGoodsSpecification> mallGoodsSpecifications = mallGoodsSpecificationMapper.selectByExample(mallGoodsSpecificationExample);

        MallGoodsProductExample mallGoodsProductExample = new MallGoodsProductExample();
        MallGoodsProductExample.Criteria mallGoodsProductExampleCriteria = mallGoodsProductExample.createCriteria();
        mallGoodsProductExampleCriteria.andGoodsIdEqualTo(id);
        mallGoodsProductExampleCriteria.andDeletedEqualTo(false);
        List<MallGoodsProduct> mallGoodsProducts = mallGoodsProductMapper.selectByExample(mallGoodsProductExample);

        GoodsDetailVO goodsDetailVO = new GoodsDetailVO(categoryIds, mallGoods, mallGoodsAttributes, mallGoodsSpecifications, mallGoodsProducts);

        return goodsDetailVO;
    }

    /**
     * @Author harvey
     * @description: 用来实现添加商品的操作
     * @Date 2021/7/9 16:46
     * url: http://182.92.235.201:8083/admin/goods/create
     */
    @Override
    public Integer insertGoods(CreateGoodsBO createGoodsBO) {
        List<MallGoodsAttribute> attributes = createGoodsBO.getAttributes();
        MallGoods goods = createGoodsBO.getGoods();
        List<MallGoodsProduct> products = createGoodsBO.getProducts();
        List<MallGoodsSpecification> specifications = createGoodsBO.getSpecifications();

        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());


        if (createGoodsBO.getGoods().getGoodsSn() == null || createGoodsBO.getGoods().getGoodsSn().equals("")) {
            return 5;
        }

        if (createGoodsBO.getGoods().getName() == null || createGoodsBO.getGoods().getName().equals("")) {
            return 1;
        }

        // 如果返回的是2，则代表商品参数表为空
        if (createGoodsBO.getAttributes() == null || createGoodsBO.getAttributes().size() == 0) {
            return 2;
        }

        // 如果返回的是2，则代表商品货品表为空
        if (createGoodsBO.getProducts() == null && createGoodsBO.getProducts().size() == 0) {
            return 3;
        }

        // 如果返回的是3，则代表商品规格表为空
        if (createGoodsBO.getSpecifications() == null && createGoodsBO.getSpecifications().size() == 0) {
            return 4;
        }

        // 判断同名操作
        MallGoodsExample mallGoodsExample = new MallGoodsExample();
        MallGoodsExample.Criteria criteria = mallGoodsExample.createCriteria();
        criteria.andNameEqualTo(goods.getName());
        criteria.andDeletedEqualTo(false);
        List<MallGoods> goodsList = mallGoodsMapper.selectByExample(mallGoodsExample);
        if (goodsList.size() == 1) {
            return 611;
        }

        mallGoodsMapper.insertSelective(goods);
        // 在这之前得先把商品给插入到数据库中获取主键的id
        for (MallGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attribute.setAddTime(new Date());
            attribute.setUpdateTime(new Date());
            mallGoodsAttributeMapper.insertSelective(attribute);
        }
        for (MallGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            product.setId(null);
            product.setAddTime(new Date());
            product.setUpdateTime(new Date());
            mallGoodsProductMapper.insertSelective(product);
        }
        for (MallGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specification.setAddTime(new Date());
            specification.setUpdateTime(new Date());
            mallGoodsSpecificationMapper.insertSelective(specification);
        }
        return 0;
    }


    /*@Override
    public void updateGoodsById(CreateGoodsBO createGoodsBO) {
        MallGoods goods = createGoodsBO.getGoods();
        goods.setUpdateTime(new Date());
        mallGoodsMapper.updateByPrimaryKeySelective(goods);

        List<MallGoodsSpecification> specifications = createGoodsBO.getSpecifications();
        for (MallGoodsSpecification specification : specifications) {
            specification.setUpdateTime(new Date());
            mallGoodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }

        List<MallGoodsProduct> products = createGoodsBO.getProducts();
        for (MallGoodsProduct product : products) {
            product.setUpdateTime(new Date());
            mallGoodsProductMapper.updateByPrimaryKeySelective(product);
        }

        List<MallGoodsAttribute> attributes = createGoodsBO.getAttributes();
        for (MallGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(new Date());
            mallGoodsAttributeMapper.updateByPrimaryKeySelective(attribute);
        }

    }*/

    /**
     * @Author harvey
     * @description: 实现更新商品的操作
     * @Date 2021/7/9 17:30
     * url: http://182.92.235.201:8083/admin/goods/update
     */
    public void updateGoodsById(CreateGoodsBO createGoodsBO) {
        MallGoods goods = createGoodsBO.getGoods();
        goods.setUpdateTime(new Date());
        mallGoodsMapper.updateByPrimaryKeySelective(goods);

        MallGoodsSpecificationExample specificationExample = new MallGoodsSpecificationExample();
        MallGoodsSpecificationExample.Criteria criteria = specificationExample.createCriteria();
        criteria.andGoodsIdEqualTo(goods.getId());
        List<MallGoodsSpecification> goodsSpecifications = mallGoodsSpecificationMapper.selectByExample(specificationExample);
        for (MallGoodsSpecification goodsSpecification : goodsSpecifications) {
            goodsSpecification.setDeleted(true);
            mallGoodsSpecificationMapper.updateByPrimaryKeySelective(goodsSpecification);
        }
        List<MallGoodsSpecification> specifications = createGoodsBO.getSpecifications();
        for (MallGoodsSpecification specification : specifications) {
            if (specification.getId() == null) {
                specification.setGoodsId(goods.getId());
                specification.setAddTime(new Date());
                specification.setUpdateTime(new Date());
                mallGoodsSpecificationMapper.insertSelective(specification);
                continue;
            }
            for (MallGoodsSpecification goodsSpecification : goodsSpecifications) {
                if (specification.getId().equals(goodsSpecification.getId())) {
                    specification.setDeleted(false);
                    specification.setUpdateTime(new Date());
                    mallGoodsSpecificationMapper.updateByPrimaryKeySelective(specification);
                }
            }
        }


        MallGoodsProductExample goodsProductExample = new MallGoodsProductExample();
        MallGoodsProductExample.Criteria criteria1 = goodsProductExample.createCriteria();
        criteria1.andGoodsIdEqualTo(goods.getId());
        List<MallGoodsProduct> mallGoodsProducts = mallGoodsProductMapper.selectByExample(goodsProductExample);
        for (MallGoodsProduct mallGoodsProduct : mallGoodsProducts) {
            mallGoodsProduct.setDeleted(true);
            mallGoodsProductMapper.updateByPrimaryKeySelective(mallGoodsProduct);
        }
        List<MallGoodsProduct> products = createGoodsBO.getProducts();
        for (MallGoodsProduct product : products) {
            if (product.getId() == null) {
                product.setGoodsId(goods.getId());
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                mallGoodsProductMapper.insertSelective(product);
                continue;
            }
            for (MallGoodsProduct mallGoodsProduct : mallGoodsProducts) {
                if (product.getId().equals(mallGoodsProduct.getId())) {
                    product.setDeleted(false);
                    product.setUpdateTime(new Date());
                    mallGoodsProductMapper.updateByPrimaryKeySelective(product);
                }
            }
        }

        MallGoodsAttributeExample goodsAttributeExample = new MallGoodsAttributeExample();
        MallGoodsAttributeExample.Criteria criteria2 = goodsAttributeExample.createCriteria();
        criteria2.andGoodsIdEqualTo(goods.getId());
        List<MallGoodsAttribute> mallGoodsAttributes = mallGoodsAttributeMapper.selectByExample(goodsAttributeExample);
        for (MallGoodsAttribute mallGoodsAttribute : mallGoodsAttributes) {
            mallGoodsAttribute.setDeleted(true);
            mallGoodsAttributeMapper.updateByPrimaryKeySelective(mallGoodsAttribute);
        }
        List<MallGoodsAttribute> attributes = createGoodsBO.getAttributes();
        for (MallGoodsAttribute attribute : attributes) {
            if (attribute.getId() == null) {
                attribute.setGoodsId(goods.getId());
                attribute.setAddTime(new Date());
                attribute.setUpdateTime(new Date());
                mallGoodsAttributeMapper.insertSelective(attribute);
                continue;
            }
            for (MallGoodsAttribute mallGoodsAttribute : mallGoodsAttributes) {
                if (attribute.getId().equals(mallGoodsAttribute.getId())) {
                    attribute.setDeleted(false);
                    attribute.setUpdateTime(new Date());
                    mallGoodsAttributeMapper.updateByPrimaryKeySelective(attribute);
                }
            }
        }
    }

    /**
     * @Author harvey
     * @description: 实现删除商品的操作
     * @Date 2021/7/9 19:45
     * url: http://182.92.235.201:8083/admin/goods/delete
     */
    @Override
    public void deleteGoodsById(Integer id) {
        MallGoods mallGoods = new MallGoods();
        mallGoods.setId(id);
        mallGoods.setDeleted(true);
        mallGoodsMapper.updateByPrimaryKeySelective(mallGoods);

        MallGoodsAttributeExample attributeExample = new MallGoodsAttributeExample();
        MallGoodsAttributeExample.Criteria criteria = attributeExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<MallGoodsAttribute> mallGoodsAttributes = mallGoodsAttributeMapper.selectByExample(attributeExample);
        for (MallGoodsAttribute mallGoodsAttribute : mallGoodsAttributes) {
            mallGoodsAttribute.setDeleted(true);
            mallGoodsAttributeMapper.updateByPrimaryKeySelective(mallGoodsAttribute);
        }

        MallGoodsSpecificationExample specificationExample = new MallGoodsSpecificationExample();
        MallGoodsSpecificationExample.Criteria criteria1 = specificationExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        List<MallGoodsSpecification> mallGoodsSpecifications = mallGoodsSpecificationMapper.selectByExample(specificationExample);
        for (MallGoodsSpecification specification : mallGoodsSpecifications) {
            specification.setDeleted(true);
            mallGoodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }

        MallGoodsProductExample productExample = new MallGoodsProductExample();
        MallGoodsProductExample.Criteria criteria2 = productExample.createCriteria();
        criteria2.andGoodsIdEqualTo(id);
        List<MallGoodsProduct> mallGoodsProducts = mallGoodsProductMapper.selectByExample(productExample);
        for (MallGoodsProduct mallGoodsProduct : mallGoodsProducts) {
            mallGoodsProduct.setDeleted(true);
            mallGoodsProductMapper.updateByPrimaryKeySelective(mallGoodsProduct);
        }
    }
}















